package org.example.friend.controller;

import org.example.entity.Base.Result;
import org.example.friend.entity.Friend_R;
import org.example.friend.entity.friend;
import org.example.friend.service.FriendService;
import org.example.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Validated
@RestController
@RequestMapping("/friend")
public class FriendController {
    private final FriendService friendService;

    public FriendController(FriendService friendService) {
        this.friendService = friendService;
    }

    @GetMapping("/getFriendList")
    public Result getFriendList(HttpServletRequest request){//@Pattern(regexp="^\\s{5,16}$")
    //根据用户邮箱查询用户
        String token=request.getHeader("Authorization");
        Map<String, Object> c= JWTUtil.parseToken(token);
        String Email=(String)(c.get("Email"));
        System.out.println(Email);
        List<friend> FriendList=friendService.getFriendList(Email);
        if (FriendList==null){
            return Result.error("无好友，查询失败");
        }
        return  Result.success(FriendList);
    }
    @DeleteMapping ("/deleteFriend")
    public Result deleteFriend(HttpServletRequest request,String friendEmail){
      System.out.println(friendEmail);
      String token=request.getHeader("Authorization");
      Map<String, Object> c= JWTUtil.parseToken(token);
      String Email=(String)(c.get("Email"));
      friendService.deleteFriend(Email,friendEmail);
      return Result.success("删除成功");
    }
    @PostMapping("/addFriend")
    public Result addFriend(HttpServletRequest request,@RequestBody Map<String,Object> map){
          String friendEmail= (String) map.get("friendEmail");
          System.out.println(friendEmail);
          String token=request.getHeader("Authorization");
          Map<String, Object> c= JWTUtil.parseToken(token);
          String Email=(String)(c.get("Email"));
          friendService.addFriend(Email,friendEmail);
          return Result.success("添加成功");
    }


    @GetMapping("/findUserByEmail")
    public Result findUserByEmail(HttpServletRequest request,String userEmail){

      String token=request.getHeader("Authorization");
      Map<String, Object> c= JWTUtil.parseToken(token);
      String Email=(String)(c.get("Email"));
      friend friend=friendService.findUserByEmail(Email,userEmail);
      if (friend==null){
          return Result.error("用户不存在，或已添加好友");
      }
      return Result.success(friend);
    }
    @GetMapping("/findFriendByEmail")
    public Result findFriendByEmail(HttpServletRequest request,String userEmail){
        System.out.println(userEmail);
        String token=request.getHeader("Authorization");
        Map<String, Object> c= JWTUtil.parseToken(token);
        String Email=(String)(c.get("Email"));
        friend friend=friendService.findFriendByEmail(Email,userEmail);
        if (friend==null){
            return Result.error("用户不存在，或未添加好友");
        }
        return Result.success(friend);
    }
    @GetMapping("/getMyFriendRequest")
    public Result getMyFriendRequest(HttpServletRequest request){
        String token=request.getHeader("Authorization");
        Map<String, Object> c= JWTUtil.parseToken(token);
        String Email=(String)(c.get("Email"));
        List<Friend_R> Friend_RList=friendService.getMyFriendRequest(Email);
        if (Friend_RList==null){
            return Result.error("无好友请求");
        }
        return  Result.success(Friend_RList);
   }
    @GetMapping("/getMyFriendResponse")
    public Result getMyFriendResponse(HttpServletRequest request){
        String token=request.getHeader("Authorization");
        Map<String, Object> c= JWTUtil.parseToken(token);
        String Email=(String)(c.get("Email"));
        List<Friend_R> Friend_RList=friendService.getMyFriendResponse(Email);
        if (Friend_RList==null){
            return Result.error("无好友请求");
        }
        return  Result.success(Friend_RList);
    }
    @PostMapping("/addFriendRequest")
    public Result addFriendRequest(HttpServletRequest request,String anotherUserEmail,String text){
        String token=request.getHeader("Authorization");
        Map<String, Object> c= JWTUtil.parseToken(token);
        String Email=(String)(c.get("Email"));
        String UserName=(String)(c.get("Name"));
        Friend_R friend_r=new Friend_R(null,Email,UserName,anotherUserEmail,text);
        friendService.addFriendRequest(friend_r);
        return Result.success("添加成功");
    }
    @DeleteMapping("/deleteFriendRequest")
    public Result deleteFriendRequest(HttpServletRequest request,String E){
        String token=request.getHeader("Authorization");
        Map<String, Object> c= JWTUtil.parseToken(token);
        String Email=(String)(c.get("Email"));
        Map<String, String> map=new java.util.HashMap<>();
        map.put("UserEmail", E);
        map.put("anotherUserEmail", Email);
        friendService.deleteFriendRequest(map);
        return Result.success("删除成功");
    }
}
